﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace td.Core.DiscoverAddins
{
    public class DiscoverAddin
    {
        public DiscoverAddin()
        {
        }
        /// <summary>
        /// 从指定的DLL中反射出插件类，并加载
        /// </summary>
        void LoadAddin()
        {
            var files = System.IO.Directory.GetFiles(Application.StartupPath, "*.addin.dll");
            foreach (var file in files)
            {
                LoadAddinFromOneDll(file);
            }
        }
        /// <summary>
        /// 从DLL文件中加载插件，文件名要求为*.addin.dll，继承于 AbstractToolButtonCommand（Ribbion上的按钮）或AbstractViewContent（TAB页面）
        /// </summary>
        /// <param name="path"></param>
        private void LoadAddinFromOneDll(string path)
        {
            Assembly assembly = Assembly.LoadFrom(path);
            Type[] types = assembly.GetTypes();
            foreach (var t in types)
            {
                var obj = assembly.CreateInstance(t.ToString());
                if (obj is td.Core.AbstractToolButtonCommand)
                {
                  // AddToolButton((td.Core.AbstractToolButtonCommand)obj);
                    //AddGroupButton((td.Core.AbstractToolButtonCommand)obj);
                }
                else if (obj is td.Core.AbstractViewContent)
                {
                   // AddView((td.Core.AbstractViewContent)obj);
                }
                else if (obj is td.Core.AbstractPad)
                {
                   // AddPad((td.Core.AbstractPad)obj);
                }
            }
        }
    }
}
